<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
  public function index()
  {
    $page = User::latest()->paginate(10);

    return success($page);
  }

  public function enable(User $user, Request $request)
  {
    $user->enable = $request->enable;
    $user->save();
    return success($user);
  }

  public function setNotifyUrl(Request $request)
  {
    $request->validate([
      'text' => 'required|url'
    ]);

    $seller = seller();
    $seller->notify_url = trim($request->text);
    $seller->save();

    return success('ok');
  }

  // public function bind(Request $request)
  // {
  //   $user = auth()->user();

  //   if ($request->has('mobile')) {
  //     $mobile = $request->mobile;

  //     $code = \Cache::pull($mobile);
  //     if (!$code) {
  //       fail('验证码已过期');
  //     }
  //     if ($code != $request->code) {
  //       fail('验证码错误');
  //     }
  //     $existsM = User::where('mobile', $mobile)->exists();
  //     if ($existsM) {
  //       fail('手机号已绑定');
  //     }
  //     $user->mobile = $mobile;
  //   }

  //   if ($request->has('bank')) {
  //     $existsB = User::where('bankcard', $request->bankcard)->exists();
  //     if ($existsB) {
  //       fail('银行卡已绑定');
  //     }
  //     $user->bank = $request->bank;
  //     $user->bankcard = $request->bankcard;
  //     $user->realname = $request->realname;
  //   }
  //   $user->save();
  //   return success('ok');
  // }

  public function profileInfo()
  {
    return success([
      'api_switch' => sys('api_switch'),
      'api_token' => auth()->user()->api_token,
      'code' => auth()->user()->code
    ]);
  }
}
